package com.thinkcms.security.custom;

import com.thinkcms.core.utils.ApiResult;
import com.thinkcms.core.utils.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException e) throws IOException, ServletException {
        log.error("------------用户权限异常-------------");
		response.setStatus(HttpStatus.FORBIDDEN.value());
		WebUtil.write(response, ApiResult.result(HttpStatus.FORBIDDEN.value(),e.getMessage()));
	}

}
